package com.xilihui.xlh.business.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.ConfirmOrderEntity;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.core.util.ScreenUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/9/13.
 */

public class DistributionDialog implements View.OnClickListener {
    
    Dialog dialog;
    Activity activity;
    ImageView imageView;
    RecyclerView recyclerView;
    select select;
    
    
    ArrayList<ConfirmOrderEntity.OrderBean.PStatusArrayBean.DeliveriesBean> data = new ArrayList<>();
    BaseAdapter<ConfirmOrderEntity.OrderBean.PStatusArrayBean.DeliveriesBean> baseAdapter;
    
    public DistributionDialog(Activity activity,ArrayList<ConfirmOrderEntity.OrderBean.PStatusArrayBean.DeliveriesBean> data){
        this.activity = activity;
        this.data = data;
        dialog = new Dialog(activity,R.style.Dialog);
        dialog.setCancelable(true);
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        dialog.setCanceledOnTouchOutside(true);
        initView();
        setDialogWidth();
    }
    

    public void setDialogWidth(){
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = ScreenUtil.getScreenWidth(activity);
        dialog.getWindow().setAttributes(lp);
    }
    
    public void initView(){
        
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_distribution,null);
        imageView = (ImageView)view.findViewById(R.id.iv);
        recyclerView = (RecyclerView)view.findViewById(R.id.recyclerview);
        imageView.setOnClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        
        baseAdapter = new BaseAdapter<ConfirmOrderEntity.OrderBean.PStatusArrayBean.DeliveriesBean>(activity,data) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_dialog_distribution;
            }

            @Override
            protected void getView(BaseViewHolder holder, final ConfirmOrderEntity.OrderBean.PStatusArrayBean.DeliveriesBean entity, final int position) {
                holder.setText(R.id.tv_type,entity.getName());
                CheckBox checkBox = (CheckBox)holder.getView(R.id.checkbox);
                
                if(entity.isFlag()){
                    checkBox.setVisibility(View.VISIBLE);
                }
                
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        select.select(entity.getName(),entity.getAgent_id()+"",position);
                        dialog.dismiss();
                    }
                });
            }
        };
        
        recyclerView.setAdapter(baseAdapter);
        baseAdapter.setAnimationsLocked(true);
        dialog.setContentView(view);
        
    }
    
    public void show(){
        dialog.show();
    }
    
    public void dismiss(){
        dialog.dismiss();
    }

    @Override
    public void onClick(View view) {
        dialog.dismiss();
    }
    
    public void setOnSelectItem(select select){
        this.select = select;
    }
    
    public interface select{
        void select(String name,String agend_id,int pos);
    }
    
}
